package com.thh.datamonitor.controller;

import java.util.List;

import com.thh.common.utils.StringUtils;
import com.thh.common.utils.bean.BeanUtils;
import com.thh.common.core.model.entity.SysUser;
import com.thh.datamonitor.dto.TmdUserDatabaseDto;
import com.thh.datamonitor.dto.TmdUserDatabaseEditDto;
import com.thh.datamonitor.dto.TmdUserDatabaseQueryDto;
import com.thh.datamonitor.entity.TmdDatabase;
import com.thh.datamonitor.service.ITmdDatabaseService;
import com.thh.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.thh.common.annotation.Log;
import com.thh.common.enums.BusinessType;
import com.thh.datamonitor.entity.TmdUserDatabase;
import com.thh.datamonitor.service.ITmdUserDatabaseService;
import com.thh.common.core.controller.BaseController;
import com.thh.common.core.model.AjaxResult;
import com.thh.common.utils.poi.ExcelUtil;
import com.thh.common.core.page.TableDataInfo;

/**
 * 用户数据库绑定信息Controller
 * 
 * @author thh
 * @date 2022-06-23
 */
@Controller
@RequestMapping("/datamonitor/userDatabase")
public class UserDatabaseController extends BaseController
{
    private String prefix = "datamonitor/userDatabase";

    @Autowired
    private ITmdUserDatabaseService tmdUserDatabaseService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITmdDatabaseService tmdDatabaseService;

    @RequiresPermissions("datamonitor:userDatabase:view")
    @GetMapping()
    public String userDatabase()
    {
        return prefix + "/userDatabase";
    }

    /**
     * 查询用户数据库绑定信息列表
     */
    @RequiresPermissions("datamonitor:userDatabase:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TmdUserDatabaseQueryDto tmdUserDatabaseQueryDto)
    {
        startPage();
        List<TmdUserDatabaseDto> list = tmdUserDatabaseService.selectTmdUserDatabaseList(tmdUserDatabaseQueryDto);
        return getDataTable(list);
    }

    /**
     * 导出用户数据库绑定信息列表
     */
    @RequiresPermissions("datamonitor:userDatabase:export")
    @Log(title = "用户数据库绑定信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TmdUserDatabaseQueryDto tmdUserDatabaseQueryDto)
    {
        List<TmdUserDatabaseDto> list = tmdUserDatabaseService.selectTmdUserDatabaseList(tmdUserDatabaseQueryDto);
        ExcelUtil<TmdUserDatabaseDto> util = new ExcelUtil<TmdUserDatabaseDto>(TmdUserDatabaseDto.class);
        return util.exportExcel(list, "用户数据库绑定信息数据");
    }

    /**
     * 新增用户数据库绑定信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户数据库绑定信息
     */
    @RequiresPermissions("datamonitor:userDatabase:add")
    @Log(title = "用户数据库绑定信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TmdUserDatabase tmdUserDatabase)
    {
        return toAjax(tmdUserDatabaseService.insertTmdUserDatabase(tmdUserDatabase));
    }

    /**
     * 修改用户数据库绑定信息
     */
    @RequiresPermissions("datamonitor:userDatabase:edit")
    @GetMapping("/edit/{userDatabaseId}")
    public String edit(@PathVariable("userDatabaseId") Long userDatabaseId, ModelMap mmap)
    {
        TmdUserDatabase tmdUserDatabase = tmdUserDatabaseService.selectTmdUserDatabaseByUserDatabaseId(userDatabaseId);
        TmdUserDatabaseEditDto tmdUserDatabaseEditDto = null;
        if (StringUtils.isNotNull(tmdUserDatabase)) {
            tmdUserDatabaseEditDto = new TmdUserDatabaseEditDto();
            BeanUtils.copyBeanProp(tmdUserDatabase, tmdUserDatabaseEditDto);
            SysUser sysUser = sysUserService.selectUserByLoginName(tmdUserDatabase.getUserId());
            TmdDatabase tmdDatabase = tmdDatabaseService.selectTmdDatabaseByDatabaseId(tmdUserDatabase.getDatabaseId());
            tmdUserDatabaseEditDto.setDatabase(tmdDatabase);
            tmdUserDatabaseEditDto.setSysUser(sysUser);
        }
        mmap.put("tmdUserDatabase", tmdUserDatabaseEditDto);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户数据库绑定信息
     */
    @RequiresPermissions("datamonitor:userDatabase:edit")
    @Log(title = "用户数据库绑定信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TmdUserDatabase tmdUserDatabase)
    {
        return toAjax(tmdUserDatabaseService.updateTmdUserDatabase(tmdUserDatabase));
    }

    /**
     * 删除用户数据库绑定信息
     */
    @RequiresPermissions("datamonitor:userDatabase:remove")
    @Log(title = "用户数据库绑定信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tmdUserDatabaseService.deleteTmdUserDatabaseByUserDatabaseIds(ids));
    }
}
